unit oEditorPedestrianManager;

interface

uses
  oManager, oProcessItem, oPedestriansManager, oProcessObjects, oFramePedestrian,
  SysUtils, oProcessObjectConstants, oFormPedestrian;

const
  edPedNew = 1100;
  edPedEdit = 2100;

  type
    EEditorPedestrianManager = class(Exception);

    TEditorPedestrianManager = class(TManager)
     private
       aFrame: TfPedestrian;
       aForm: TfrmPedestrian;
       procedure clear;
       procedure defaults;
       procedure show(paData : TPedestrianManager.TPedestrianType;paFormType: Integer);
       procedure fillForm(paData: TPedestrianManager.TPedestrianType);
     public
       constructor Create(paMaster: TManager);
       function ProcessObject(paItem: TProcessItem):TProcessItem; override; final;
    end;

implementation

uses
  Dialogs, Controls, oMasterManager;

resourcestring eEdPedManNotValidType = 'not a valid processing type';
resourcestring eEdPedManNotFound = 'pedestrian not found';

{ TEditorPedestrianManager }

procedure TEditorPedestrianManager.clear;
begin
  with aForm.aFrame do
    begin
      txtName.Text := '';
      txtCrowdTightning.Text := '';
      txtGainTightning.Text := '';
      txtBarierTightning.Text := '';
      txtDiameter.Text := '';
      txtAvgVelocity.Text := '';
      txtMaxVelocity.Text := '';
      txtMaxAcceleration.Text := '';
      txtView.Text := '';
    end;
end;

constructor TEditorPedestrianManager.Create(paMaster: TManager);
begin
  aMaster := paMaster;
end;

procedure TEditorPedestrianManager.defaults;
begin
  with aForm.aFrame do
    begin
      txtName.Text := 'pedestrian' + IntToStr((Master as TMasterManager).PedestriansManager.Pedestrians.Count + 1);
      txtCrowdTightning.Text := '0';
      txtGainTightning.Text := '4';
      txtBarierTightning.Text := '-1';
      txtDiameter.Text := FloatToStr((Master as tMasterManager).SceneManager.Scene.getMinimumCellSize);
      txtAvgVelocity.Text := '1';
      txtMaxVelocity.Text := '1';
      txtMaxAcceleration.Text := '1';
      txtView.Text := '10';
    end;
end;

procedure TEditorPedestrianManager.fillForm(paData: TPedestrianManager.TPedestrianType);
begin
  if aForm = nil then exit;
  aFrame := aForm.aFrame;
  clear;
  if paData <> nil then
    begin
      aFrame.txtName.Text := paData.Name;
      aFrame.txtCrowdTightning.Text := FloatToStr(paData.CrowdTightning);
      aFrame.txtGainTightning.Text := FloatToStr(paData.GainTightning);
      aFrame.txtBarierTightning.Text := FloatToStr(paData.BarierTightning);
      aFrame.txtDiameter.Text := FloatToStr(paData.Diameter);
      aFrame.txtAvgVelocity.Text := FloatToStr(paData.AvgVelocity);
      aFrame.txtMaxVelocity.Text := FloatToStr(paData.MaxVelocity);
      aFrame.txtMaxAcceleration.Text := FloatToStr(paData.MaxAcceleration);
      aFrame.txtView.Text := FloatToStr(paData.View);
    end;
end;

function TEditorPedestrianManager.ProcessObject(
  paItem: TProcessItem): TProcessItem;
var
  item: TProcessItem;
begin
  case paItem.Code of

    PO_EDITOR_PED_INSERT: begin
      show(nil,edPedNew);
      paItem.Free;
      result := nil;
    end;

    PO_EDITOR_PED_EDIT: begin
      try
        if not(paItem.Data is TString) then raise EEditorPedestrianManager.Create(eEdPedManNotValidType);
        item := Master.ProcessObject(TProcessItem.create(PO_PEDMAN_EXISTS,paItem.Data as TString));
        if item.Data = nil then raise EEditorPedestrianManager.Create(eEdPedManNotFound);
        show(item.Data as TPedestrianManager.TPedestrianType,edPedEdit);
        result := nil;
      except
        Result := TProcessItem.create(PO_FAILURE,nil);
      end;
      paItem.Free;
    end;

  end;
end;

procedure TEditorPedestrianManager.show(paData: TPedestrianManager.TPedestrianType;
  paFormType: Integer);
var
  mr: Integer;
  pedData: TPedestrianData;
begin
  aForm := TfrmPedestrian.Create(nil);
  aForm.Master := aMaster;
  fillForm(paData);
  case paFormType of
    edPedNew: begin
      defaults;
      aForm.aFrame.btnOK.Visible := true;
      aForm.aFrame.btnEdit.Visible := false;
      aForm.aFrame.txtName.Enabled := true;
    end;
    edPedEdit: begin
      aForm.aFrame.btnOK.Visible := false;
      aForm.aFrame.btnEdit.Visible := true;
      aForm.aFrame.txtName.Enabled := false;
    end;
  end;
  mr := aForm.ShowModal;
  case mr of
    mrOk: begin
      pedData := TPedestrianData.Create(aFrame.txtName.Text,StrToFloat(aFrame.txtCrowdTightning.Text),
        StrToFloat(aFrame.txtGainTightning.Text),StrToFloat(aFrame.txtBarierTightning.Text),
        StrToFloat(aFrame.txtDiameter.Text),StrToFloat(aFrame.txtAvgVelocity.Text),
        StrToFloat(aFrame.txtMaxVelocity.Text),StrToFloat(aFrame.txtMaxAcceleration.Text),
        StrToFloat(aFrame.txtView.Text));
      aMaster.ProcessObject(TProcessItem.create(PO_PEDMAN_INSERT,pedData));
    end;
    mrCancel: begin
    end;
    mrYes: begin
      pedData := TPedestrianData.Create(aFrame.txtName.Text,StrToFloat(aFrame.txtCrowdTightning.Text),
        StrToFloat(aFrame.txtGainTightning.Text),StrToFloat(aFrame.txtBarierTightning.Text),
        StrToFloat(aFrame.txtDiameter.Text),StrToFloat(aFrame.txtAvgVelocity.Text),
        StrToFloat(aFrame.txtMaxVelocity.Text),StrToFloat(aFrame.txtMaxAcceleration.Text),
        StrToFloat(aFrame.txtView.Text));
      aMaster.ProcessObject(TProcessItem.create(PO_PEDMAN_EDIT,pedData));
    end;
  end;
  aForm.Free;
  aForm := nil;
  aFrame := nil;
end;

end.
